package com.leo.common;

import com.leo.bean.EmailInfo;
import com.leo.db.DaoMapperProxy;
import com.leo.mapper.UserMapper;
import com.leo.view.MessageContentView;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author wangss
 * @date 2023/02/22 18:59
 * @description
 **/
public class MailViewUtils {

    /**
     * 收件箱面板
     **/
    public static TreeView inBoxView;
    /**
     * 草稿箱面板
     **/
    public static TreeView draftBoxView;
    /**
     * 发件箱面板
     **/
    public static TreeView sendBoxView;
    /**
     * 垃圾箱面板
     **/
    public static TreeView dustbinView;

    public static TreeItem inBoxRoot;
    public static TreeItem draftBoxRoot;
    public static TreeItem sendBoxRoot;
    public static TreeItem dustbinRoot;

    static {
        inBoxRoot = new TreeItem("收件箱");
        inBoxView = new TreeView(inBoxRoot);
        inBoxView.setPrefHeight(758);
        inBoxRoot.setExpanded(true);
        inBoxView.setShowRoot(false);

        draftBoxRoot = new TreeItem("草稿箱");
        draftBoxView = new TreeView(draftBoxRoot);
        draftBoxView.setPrefHeight(758);
        draftBoxRoot.setExpanded(true);
        draftBoxView.setShowRoot(false);

        sendBoxRoot = new TreeItem("发件箱");
        sendBoxView = new TreeView(sendBoxRoot);
        sendBoxView.setPrefHeight(758);
        sendBoxRoot.setExpanded(true);
        sendBoxView.setShowRoot(false);

        dustbinRoot = new TreeItem("垃圾箱");
        dustbinView = new TreeView(dustbinRoot);
        dustbinView.setPrefHeight(758);
        dustbinRoot.setExpanded(true);
        dustbinView.setShowRoot(false);
    }

    /**
     * 添加用户邮箱列表
     *
     * @param emailInfos
     * @param root
     */
    public static void addMailUserList(List<EmailInfo> emailInfos, TreeItem root, StackPane emailContentPane) {
        for (EmailInfo mail : emailInfos) {
            TreeItem treeItem = new TreeItem(mail.getEmail());
            treeItem.setExpanded(true);
            TreeItem inBox = new TreeItem("收件箱");
            TreeItem draftBox = new TreeItem("草稿箱");
            TreeItem sendBox = new TreeItem("已发送邮件");
            TreeItem dustbin = new TreeItem("垃圾箱");
            TreeItem friendBox = new TreeItem("好友列表");

            // 获取好友列表
            UserMapper userMapper = DaoMapperProxy.getProxyInstance(UserMapper.class);
            List<String> friendInfos = userMapper.selectFriendInfos();
            ApplicationContentUtil.friendList.addAll(friendInfos);
            friendInfos.stream().forEach(friendInfo -> {
                TreeItem item = new TreeItem(friendInfo);
                friendBox.getChildren().add(item);
            });

            treeItem.getChildren().addAll(inBox, draftBox, sendBox, dustbin, friendBox);
            root.getChildren().add(treeItem);
            ApplicationContentUtil.putContentMap(mail.getEmail(), mail);
            ApplicationContentUtil.EMAILLIST.add(mail.getEmail());
        }
        // 设置当前邮箱
        ApplicationContentUtil.putContentMap("currentEmail", ApplicationContentUtil.EMAILLIST.get(0));
        // 初始化内容面板（邮件内容展示）
        MessageContentView contentView = new MessageContentView();
        emailContentPane.getChildren().add(contentView);
        ApplicationContentUtil.putContentMap("contentView", contentView);
    }

    /**
     * @description: 根据选择的不同邮箱列表展示不同的邮件
     **/
    public static void changeTreeViewVisible(String name, VBox emailContentVbox) {
        emailContentVbox.getChildren().clear();

        if (StringUtils.equals(name, "收件箱")) {
            emailContentVbox.getChildren().add(inBoxView);
        } else if (StringUtils.equals(name, "草稿箱")) {
            emailContentVbox.getChildren().add(draftBoxView);
        } else if (StringUtils.equals(name, "已发送邮件")) {
            emailContentVbox.getChildren().add(sendBoxView);
        } else if (StringUtils.equals(name, "垃圾箱")) {
            emailContentVbox.getChildren().add(dustbinView);
        }
    }
}
